<?php
namespace app\common\redis;

use app\common\library\CommonFun;
use app\common\service\ElkLog;

/**
 * Created by PhpStorm.
 * User: 张辉
 * Date: 2018/8/6
 * Time: 12:58
 */

/*
 * api请求随机数缓存
 */
class ApiNonceRedis extends RedisModel
{
    /**
     * Redis配置名称
     * @var string
     */
    public $redis = 'default';

    /**
     * Redis key
     * @var string
     */
    public $key = 'str:api_nonce_';

    public $article_market_report_global_key = "str:article_market_report_global_";

    /**
     * 标记未登录用户轨迹
     */
    public $activity_user_client_sn_key = "str:activity_user_client_sn_";

    /**
     * 安卓页面配置
     * @var string
     */
    public $android_page_config_by_version = "hash:android_page_config_by_version";

    /**
     * ios页面配置
     */
    public $ios_page_config_by_version = "hash:ios_page_config_by_version";

    /**
     * 用户点赞缓存
     */
    public $user_up_comment_limit_key = "str:user_up_comment_limit_";

    /**
     * 用户点赞缓存
     */
    public $user_follow_user_limit_key = "str:user_follow_user_limit_";

    /**
     * 用户分享缓存
     */
    public $user_share_comment_limit_key = "str:user_share_comment_limit_";

    /**
     * 用户推送 push_cache
     */
    public $user_community_operation_type_push_key = "str:user_community_operation_type_push_";

    /**
     * 用户推送 push_cache_list
     */
    public $user_community_operation_type_push_list_key = "set:user_community_operation_type_push_";

    /**
     * 标记用户
     * @param $client_sn
     * @return bool
     */
    public function set_activity_user_client_sn( $client_sn )
    {
        return $this->Set( 1,$this->activity_user_client_sn_key.$client_sn, 3600 );
    }

    /**
     * 获取标记用户
     * @param $client_sn
     * @return bool|mixed|string
     */
    public function get_activity_user_client_sn( $client_sn )
    {
        return $this->Get( $this->activity_user_client_sn_key.$client_sn );
    }

    /**
     * 成功之后标记用户
     * @param $client_sn
     * @return int
     */
    public function delete_activity_user_client_sn( $client_sn )
    {
        return $this->Delete( $this->activity_user_client_sn_key.$client_sn );
    }

    /**
     * 标记活动用户
     * @param $user_id
     * @param $coin_id
     * @return bool
     */
    public function set_invite_friends_follow_coin( $user_id, $coin_id )
    {
        ElkLog::write( array( "message" => "set_invite_friends_follow_coin") );
        return $this->Set( $coin_id,"str:invite_friends_follow_coin_by{$user_id}", 1200 );
    }

    /**
     * 获取标记用户
     * @param $user_id
     * @return bool|string
     */
    public function get_invite_friends_follow_coin( $user_id )
    {
        $coin_id =  $this->Get( "str:invite_friends_follow_coin_by{$user_id}" );
        if ( $coin_id ){
            ElkLog::write( array( "message" => "get_invite_friends_follow_coin2", "user_id" => $user_id, "coin_id" => $coin_id) );
            $this->Delete( "str:invite_friends_follow_coin_by{$user_id}" );
        } else {
            ElkLog::write( array( "message" => "get_invite_friends_follow_coin3", "user_id" => $user_id, "coin_id" => $coin_id) );
        }
        return $coin_id;
    }

    /**
     * 检测返回客户端数据结构
     * @param $os_type
     * @param $version
     * @return array|mixed
     */
    public function get_page_config( $os_type, $version )
    {
        $key = $os_type == "android" ? $this->android_page_config_by_version : $this->ios_page_config_by_version;
        $page_list = $this->HashGetByField( $version, $key );
        $page_list = $page_list ? json_decode( $page_list, true ) : array();
        return $page_list;
    }

    /**
     * 设置客户端页面结构
     * @param $os_type
     * @param $version
     * @param $list
     * @return mixed
     */
    public function set_page_config( $os_type, $version ,$list )
    {
        $key = $os_type == "android" ? $this->android_page_config_by_version : $this->ios_page_config_by_version;
        return $this->HashSetField( $version, CommonFun::json_encode2( $list ), $key );
    }

    /**
     * 获取用户点赞时缓存key
     * @param $user_id
     * @param $comment_id
     * @return string
     */
    public function get_user_up_comment_redis_key( $user_id, $comment_id )
    {
        return $this->user_up_comment_limit_key.$user_id."_{$comment_id}";
    }

    /**
     * 检测该点赞是否生效
     * @param $user_id
     * @param $comment_id
     * @return bool
     */
    public function check_user_up_comment( $user_id, $comment_id )
    {
        $redis_key = $this->get_user_up_comment_redis_key( $user_id, $comment_id );

        // 增加文章推送缓存
        if ( !$this->Setnx( 1, $redis_key ) ){
            // 检测3分钟内有推送
            return false;
        }

        $this->Expire( 86400, $redis_key );
        return true;
    }

    /**
     * 获取用户分享时缓存key
     * @param $user_id
     * @param $comment_id
     * @return string
     */
    public function get_user_share_comment_redis_key( $user_id, $comment_id )
    {
        return $this->user_share_comment_limit_key.$user_id."_{$comment_id}";
    }

    /**
     * 检测该分享是否生效
     * @param $user_id
     * @param $comment_id
     * @return bool
     */
    public function check_user_share_comment( $user_id, $comment_id )
    {
        $redis_key = $this->get_user_share_comment_redis_key( $user_id, $comment_id );

        // 增加文章推送缓存
        if ( !$this->Setnx( 1, $redis_key ) ){
            // 检测3分钟内有推送
            return false;
        }

        $this->Expire( 86400, $redis_key );
        return true;
    }

    /**
     * 获取用户关注时缓存
     * @param $user_id
     * @param $follow_user_id
     * @return string
     */
    public function get_follow_user_redis_key( $user_id, $follow_user_id )
    {
        return $this->user_follow_user_limit_key.$user_id."_{$follow_user_id}";
    }

    /**
     * 检测该关注是否生效
     * @param $user_id
     * @param $follow_user_id
     * @return bool
     */
    public function check_user_follow_user( $user_id, $follow_user_id )
    {
        if ( !$user_id ){
            return true;
        }

        $redis_key = $this->get_follow_user_redis_key( $user_id, $follow_user_id );

        // 增加文章推送缓存
        if ( !$this->Setnx( 1, $redis_key ) ){
            // 检测3分钟内有推送
            return false;
        }

        $this->Expire( 86400, $redis_key );
        return true;
    }

    /**
     * 获取用户推送缓存cache
     * @param $user_id
     * @param $type
     * @return bool|string
     */
    public function get_user_community_operation_type_push( $user_id, $type )
    {
        return $this->Get( $this->user_community_operation_type_push_key.$user_id."_".$type );
    }

    /**
     * 设置用户推送缓存
     * @param $user_id
     * @param $type
     * @return bool
     */
    public function set_user_community_operation_type_push( $user_id, $type  )
    {
        return $this->Set( 1,$this->user_community_operation_type_push_key.$user_id."_".$type , 300 );
    }

    /**
     * 获取需要聚合的队列
     * @param $user_id
     * @param $type
     * @return array
     */
    public function get_user_community_operation_list_type_push( $user_id, $type )
    {
        $list =  $this->SetGetAll( $this->user_community_operation_type_push_list_key.$user_id."_".$type );
        $this->Delete( $this->user_community_operation_type_push_list_key.$user_id."_".$type  );
        return $list;
    }

    /**
     * 设置需要聚合的队列
     * @param $user_id
     * @param $type
     * @param $message_id
     * @return mixed
     */
    public function set_user_community_operation_list_type_push( $user_id, $type , $message_id )
    {
        return $this->SetSadd( $message_id, $this->user_community_operation_type_push_list_key.$user_id."_".$type );
    }



}